<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件生命周期</title>
</head>

<body>
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom库 -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入babel 转换jsx的库 -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入PropType库  min没有警告-->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <script type="text/babel">/* 此处要写babel */
        // 创建组件
        class Demo extends React.Component {
            state = { opacity: 1 }
            componentDidMount() {
                console.log('组件挂载完成');

                setInterval(() => {
                    let { opacity } = this.state
                    if (opacity <= 0) {
                        opacity = 1
                    }
                    this.setState({
                        opacity: opacity -= 0.1
                    })
                }, 500);
            }
            // 卸载组件
            componentWillUnmount() {
                // root.unmount();
                console.log('componentWillUnmount');
            }

            unStudy = () => {
                root.unmount();
                // ReactDOM.unmountComponentAtNode(document.getElementById('test'));
            }

            // 渲染组件
            render() {
                return (
                    <>
                        <h2 style={{ opacity: this.state.opacity }}>React学不会怎么办？</h2>
                        <button onClick={this.unStudy}>不学了</button>
                    </>
                )
            }
        }

        const root = ReactDOM.createRoot(document.getElementById('test'));
        root.render(<Demo />);


    </script>
</body>

</html>